package com.hxd.otc.module.my.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.my.contract.ModifyLoginPasswordContract;
import com.hxd.otc.module.my.presenter.ModifyLoginPasswordPresenter;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.AutoSplitOnTvGlobalLayoutAdapter;
import com.hxd.otc.widget.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/6/12 0012.
 * description ${修改登陆密码}
 */
public class ModifyLoginPasswordActivity extends BaseActivity<ModifyLoginPasswordPresenter> implements ModifyLoginPasswordContract.View {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.tv_explain)
    TextView mTvExplain;
    @BindView(R.id.et_old_password)
    EditText mEtOldPassword;
    @BindView(R.id.et_confirm_password)
    EditText mEtConfirmPassword;
    @BindView(R.id.et_new_password)
    EditText mEtNewPassword;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_login_password;
    }

    @Override
    protected ModifyLoginPasswordPresenter createPresenter() {
        return new ModifyLoginPasswordPresenter(this);
    }

    @Override
    protected void initData() {
        UIUtils.setLoginPasswordEditText(mEtOldPassword);
        UIUtils.setLoginPasswordEditText(mEtNewPassword);
        mEtNewPassword.setHint(getString(R.string.my_modify_new_password_require, Constant.LOGIN_PASSWORD_MIN_LENGTH, Constant.LOGIN_PASSWORD_MAX_LENGTH));
        UIUtils.setLoginPasswordEditText(mEtConfirmPassword);
        mTvExplain.getViewTreeObserver().addOnGlobalLayoutListener(new AutoSplitOnTvGlobalLayoutAdapter(mTvExplain));
    }

    @OnClick({R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                onConfirm();
                break;
        }
    }

    private void onConfirm() {
        String oldPassword = mEtOldPassword.getText().toString().trim();
        if (!DataUtils.isLoginPassword(oldPassword, getString(R.string.old_password))) {
            mEtOldPassword.requestFocus();
            return;
        }

        String newPassword = mEtNewPassword.getText().toString().trim();
        if (!DataUtils.isLoginPassword(newPassword, getString(R.string.new_password))) {
            mEtNewPassword.requestFocus();
            return;
        }

        String confirmPassword = mEtConfirmPassword.getText().toString().trim();
        if (!DataUtils.isLoginPassword(confirmPassword, getString(R.string.my_confirm_password))) {
            mEtConfirmPassword.requestFocus();
            return;
        }

        if (!newPassword.equals(confirmPassword)) {
            showToast(getString(R.string.two_input_password_not_sure));
            return;
        }

        mPresenter.modifyLoginPassword(newPassword, oldPassword);
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void modifyLoginPasswordSuccess() {
        showToast(getString(R.string.my_modify_login_password_success));
        finish();
    }
}
